<?php
/**
* phpCan - http://idc.anavallasuiza.com/
*
* phpCan is released under the GNU Affero GPL version 3
*
* More information at license.txt
*/

namespace data\formats;

defined('ANS') or die();

class Url extends Formats implements iFormats {
	var $format = 'url';

	public function check ($value) {
		$this->error = array();

		if ($value[''] == 'http://') {
			$value[''] = '';
		}

		if ($value[''] && !filter_var($value[''], FILTER_VALIDATE_URL)) {
			$this->error[''] = __('This is an invalid url');
			return false;
		}

		$str = strstr(trim($value['']), '://');

		if (!$str || $str == '://') {
			$value[''] = '';
		}

		if (!$this->validate($value)) {
			return false;
		}

		return true;
	}

	public function valueDB (\data\Data $Data, $value, $language = '', $id = 0) {
		if ($value['']) {
			if (strstr($value[''], '://') == '://') {
				$value[''] = '';
			} else if (!preg_match('#(http://|https://|ftp://)#', $value[''])) {
				$value[''] = 'http://'.$value[''];
			}
		}

		return $value;
	}

	public function settings ($settings) {
		$this->settings = $this->setSettings($settings, array(
			'' => array(
				'db_type' => 'varchar',

				'length_max' => 255,
			)
		));

		return $this->settings;
	}
}
?>